package com.gdth.bank.model;

import org.json.JSONException;
import org.json.JSONObject;

import com.gdth.bank.BankApplication;
import com.gdth.bank.utils.AppUtil;
import com.gdth.coder.encrypt.Encryption;
import com.gdth.database.bean.TAppYh;
import com.gdth.http.entity.HttpTokenResult;
import com.gdth.http.method.HttpMethodServlet;
import com.gdth.resource.FunctionOnOff;
import com.gdth.utils.GsonUtil;

import rx.Observable;

/** 
 * @author  arvin
 * @version 2016-05-27 11:42:50 
 * 
 */
public class LoginModel {
	
	
	public Observable<String> login(TAppYh user){
		String sjzdlx = android.os.Build.MODEL;
		String xtbb = android.os.Build.VERSION.RELEASE;
		String sjbm = BankApplication.mInstance.getDeviceId();
		if (TAppCanshuModel.isUpdateData()) {
			user.setJtzz("true");
			
		}
		user.setSjzdlx(sjzdlx);
		user.setXtbb(xtbb);
		user.setBmmc(sjbm);
		String val = GsonUtil.getGson().toJson(user);
		return HttpMethodServlet.getInstance().login(val);
	}
	
	public Observable<HttpTokenResult> getTokenForLogin(String username, String mm){
		//设置正在登录
        BankApplication.setAppState(true);
        JSONObject val=new JSONObject();
       
//        JSONObject val1=new JSONObject();
        String content = "";
    	try {
    		val.put("username", username);
    		val.put("mm", mm);
			val.put("model", AppUtil.getModelForOS());
	    	val.put("sdk", AppUtil.getVersionSDKForOS());
	    	val.put("release", AppUtil.getVersionReleaseForOS());
	    	val.put("deviceid", BankApplication.mInstance.getDeviceId());
	    	try { 
	    	 content = Encryption.encrypt(val.toString());
//    		 val1.put("content", content);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(FunctionOnOff.isOpenToken) {
			return HttpMethodServlet.getInstance().getTokenForLogin(content);
		}else {
			return HttpMethodServlet.getInstance().getTokenForLogin(val.toString());
		}
		
		
	}
	
	
}
